package com.tdk.wifidirectserver.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.tdk.wifidirectserver.R
import com.tdk.wifidirectserver.bean.MessageBean

/**
 * @Des:
 * @Author: hang.zhao
 * @Date: 2023/2/28
 */
class MessageAdapter(list: List<MessageBean>?) : RecyclerView.Adapter<MyViewHolder?>() {
    private val list: List<MessageBean>?
    override fun getItemViewType(position: Int): Int {
        val messageBean: MessageBean = list!![position]
        return messageBean.type
    }

    override fun getItemCount(): Int {
        return list?.size ?: 0
    }

    init {
        this.list = list
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        if (viewType == MessageBean.MESSAGE_RECEIVE) {
            val leftView: View = LayoutInflater.from(parent.context).inflate(R.layout.msg_left_item, parent, false)
            return LeftViewHolder(leftView)
        }
        val view: View = LayoutInflater.from(parent.context).inflate(R.layout.msg_right_item, parent, false)
        return RightViewHolder(view)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val message: MessageBean = list!![position]
        Log.d("MessageAdapter", "onBindViewHolder: $message")
        if (holder is LeftViewHolder) {
            holder.leftMsg.text = message.data
        }
        if (holder is RightViewHolder) {
            holder.rightMsg.text = message.data
        }
    }


}